package c.x.jy.wexin.controller;

import c.x.jy.common.annotation.IntoSet;
import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.R;
import c.x.jy.common.utils.SysUtils;
import c.x.jy.dao.dto.AgreeArticleEntityDto;
import c.x.jy.dao.entity.AgreeArticleDataEntity;
import c.x.jy.dao.entity.AgreeArticleEntity;
import c.x.jy.service.config.BusinessConfigService;
import c.x.jy.service.service.AgreeArticleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;


/**
 * 
 *
 * @author amwhyh
 * @email xxx@xx.com
 * @date 2021-06-15 12:01:28
 */
@RestController
@RequestMapping("wechat/agreearticle")
public class AgreeArticleController  {
    @Autowired
    private AgreeArticleService agreeArticleService;
    @Autowired
    private c.x.jy.service.service.AgreeArticleDataService AgreeArticleDataService;
    @Autowired
    private BusinessConfigService businessConfigService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:agreearticle:list")
    public R list(@RequestParam Map<String, Object> params){

        PageUtils page = agreeArticleService.queryPage(params);

        return R.put(page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		AgreeArticleEntity agreeArticle = agreeArticleService.getById(id);

        return R.put(agreeArticle);
    }
    /**
     * 根据code获取agree
     */
    @IntoSet(Includefields={"content"})
    @RequestMapping("/infobykey")
    public R infobykey(String key){
        String code = businessConfigService.getBusinessConfigValue(null,key);
        AgreeArticleEntityDto agreeArticle = agreeArticleService.getByCode(code);

        return R.put(agreeArticle);
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:agreearticle:save")
    public R save(@RequestBody AgreeArticleEntity agreeArticle){
        agreeArticle.setUpdateDate(new Date());
        agreeArticle.setCode(SysUtils.generateShortCode());
		agreeArticleService.save(agreeArticle);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:agreearticle:update")
    public R update(@RequestBody AgreeArticleEntity agreeArticle){
		agreeArticleService.updateById(agreeArticle);

        return R.ok();
    }


    /**
     * 修改类容
     */
    @RequestMapping("/datasave")
    @RequiresPermissions("sys:agreearticle:update")
    public R datasave(@RequestBody AgreeArticleDataEntity agreeArticle){


        if(agreeArticle.getId()!=null){
            AgreeArticleDataService.updateById(agreeArticle);

        }else{
            AgreeArticleDataService.save(agreeArticle);
        }
        AgreeArticleEntity article  =new AgreeArticleEntity() ;
        article.setId(agreeArticle.getTitleId());
        article.setArticleDataId(agreeArticle.getId());
        agreeArticleService.updateById( article);


        return R.ok();
    }


    /**
     * 修改类容
     */
    @RequestMapping("/datainfo/{id}")
    public R datasave(@PathVariable("id") Long id){

        return R.put( AgreeArticleDataService.getById(id));
    }



    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    @RequiresPermissions("sys:agreearticle:delete")
    public R delete(@PathVariable("id") Long id){

        AgreeArticleEntity  agreeArticleEntity=    agreeArticleService.getById(id);
        if(agreeArticleEntity!=null&&agreeArticleEntity.getArticleDataId()!=null)
        AgreeArticleDataService.removeById(agreeArticleEntity.getArticleDataId());
        if(agreeArticleEntity!=null)
		agreeArticleService.removeByIds(Arrays.asList(id));

        return R.ok();
    }

}

